import React, { Component } from 'react'
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Redirect
} from 'react-router-dom';

// 引入路由配置
import Routers from './routes/routerMap'
import NotFound from './pages/NotFound'

import { inject, observer } from 'mobx-react';

@inject('userStore')
@observer
class App extends Component {
  render() {
    let { token } = this.props.userStore.user
    return (
      <div>
        {
          <Router>
            <Switch>
              {
                Routers.map((item, index) => {
                  return (
                    <Route
                      key={index}
                      path={item.path}
                      exact
                      render={
                        props => {
                          if (!item.auth) {
                            return (<item.component {...props} />)
                          }
                          if (token) {
                            return <item.component {...props} />
                          }
                          return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
                        }
                      }
                    />
                  )
                })
              }
              {/* 所有错误路由跳转页面 */}
              <Route component={NotFound} />
            </Switch>
          </Router>
        }
      </div>
    )
  }
}
export default App